#ifndef _META_DATA_SVR_CLIENT_H_
#define _META_DATA_SVR_CLIENT_H_

#include <vector>
#include <map>

#include <log4cxx/logger.h>

#include "SocketConnPool.h"
#include "CnfSvrClient.h"

using std::vector;
using std::map;

using namespace log4cxx;


struct DATA_SVR_MD_RECORD {
        vector<uint32_t> crcs;
        int64_t file_size;
};

struct CLIENT_MD_RECORD {
        vector<uint32_t> crcs;
};

struct MD_FILE_INFO {
        map<int, DATA_SVR_MD_RECORD> data_svr_md_records;
        CLIENT_MD_RECORD client_md_record;
};

class MetadataSvrClient {
public:
	int init(const char * svr_ip, const unsigned svr_port, const int conn_pool_size, const int conn_timeout, \
                                const int send_timeout, const int recv_timeout, const int max_response_size);

	int create_md_file(int32_t dir_id, int64_t file_id);
	int delete_md_file(int32_t dir_id, int64_t file_id);
	int get_md_file_info(int32_t dir_id, int64_t file_id, MD_FILE_INFO & info);
	int data_svr_notify_data_file_info(int32_t dir_id, int64_t file_id, int data_svr_id, int64_t file_size, \
						vector<uint32_t> crcs, int delete_flag, int notify_reason);
	int client_notify_client_data_info(int32_t dir_id, int64_t file_id, vector<uint32_t> crcs);
	
	int alloc_ds_4_new_file(int32_t & ds_id, int32_t & dir_id, int64_t & file_id);

private:
	string _svr_ip;
	unsigned short _svr_port;
	int _conn_pool_size;
	int _conn_timeout;
	int _send_timeout;
	int _recv_timeout;
	int _max_response_size;
	SocketConnPool _pool;
	static LoggerPtr _logger; 
};

class MetadataSvrClientGroup {
public:
       int init(CnfSvrClient * csc);
        MetadataSvrClient * get_meta_data_svr_client(int mds_id);
private:
        pthread_mutex_t _mutex;
        map<int, MetadataSvrClient *> _clients;
        CnfSvrClient * _csc;
        static LoggerPtr _logger;
};


#endif
